package com.gopay.bis.njcb.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.njcb.ebank.query.request.NjcbeBankDataReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.OpReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.ReqParam;
import com.gopay.common.domain.bank.njcb.ebank.query.response.NjcbeBankDataRep;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class NjcbB2cQueryHelper {

	private HttpTransport httpTransport;

	private static String header = "<?xml version=\"1.0\" encoding=\"gb2312\"?>";

	public Object Query(NjcbeBankDataReq njcbeBankDataReq) throws GopayException {
		String[] signOriStr = njcbeBankDataReq.getSignMessage().split("\\|\\|");
		String signStr = signOriStr[0];
		String certSign = signOriStr[1];
		String reqDataStr = header + "<NJCBEBankData>"+njcbeBankDataReq.getPlainMessage()+"</NJCBEBankData>";
		Map<String, String> param = new HashMap<String, String>();
		param.put("opName", "API_queryEnterpriseOrdersOp");
		param.put("reqData", reqDataStr);
		param.put("signData", signStr);
		param.put("KoalB64Cert", certSign);
		BankLogUtils.printB2CQuerySendLog(njcbeBankDataReq, param);
		httpTransport.setUrl(njcbeBankDataReq.getQueryUrl());
		String queryResult = null;
		try {
			httpTransport.setSendEncoding("gb2312");
			queryResult = httpTransport.submit(param);
		} catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(njcbeBankDataReq, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
		}

		if (queryResult == null || "".equals(queryResult)) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
		}
		BankLogUtils.printB2CQueryBlackLog(njcbeBankDataReq, queryResult);
		return queryResult;
	}

	public NjcbeBankDataRep parseMessage(String resultStr) throws GopayException {
		NjcbeBankDataRep resultMessage = new NjcbeBankDataRep();
		try {
			String respStr = resultStr.substring(resultStr.indexOf("?>") + 2);
			XStream xs = new XStream(new DomDriver());
			xs.autodetectAnnotations(true);
			xs.toXML(resultMessage);
			resultMessage = (NjcbeBankDataRep) xs.fromXML(respStr);
			String plainStr = respStr.substring(respStr.indexOf("<opRep>"),
					respStr.indexOf("<signData>"));
			resultMessage.setPlainMessage(plainStr);
			return resultMessage;
		} catch (Exception e) {
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6039);
		}
	}

	public void setHttpTransport(HttpTransport httpTransport) {
		this.httpTransport = httpTransport;
	}

	public String buildQuerySignStr(NjcbeBankDataReq req) {
		OpReq opReq = req.getOpReq();
		ReqParam reqParam = opReq.getReqParam();

		String opReqStr = "<opReq><serialNo></serialNo><reqTime>"
				+ opReq.getReqTime() + "</reqTime><ReqParam><custNo>"
				+ reqParam.getCustNo()
				+ "</custNo><orderState></orderState><startDate>"
				+ reqParam.getStartDate() + "</startDate><endDate>"
				+ reqParam.getEndDate() + "</endDate><orderNo>"
				+ reqParam.getOrderNo() + "</orderNo><orderType>"
				+ reqParam.getOrderType() + "</orderType></ReqParam></opReq>";

		return opReqStr;
	}

}
